package com.noble.meether.fragment.mail;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.noble.glamour.module.logic.baseui.BaseFragment;
import com.noble.glamour.module.util.TimerUtil;
import com.noble.library.observe.MsgMgr;
import com.noble.library.observe.MsgType;
import com.noble.library.observe.PObserver;
import com.noble.library.widget.RecyclerView.base.BaseRecyclerViewHolder;
import com.noble.meether.MeetUIShow;
import com.noble.meether.R;
import com.noble.meether.fragment.mail.panel.MailMsgPanel;

import java.util.ArrayList;
import java.util.List;

/**
 * 消息
 */
public class MailFragment extends BaseFragment implements BaseRecyclerViewHolder.OnItemClickListener, PObserver {
    private MailFragmentAdapter adapter;
    private MailMsgPanel mailMsgPanel;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        setContentView(R.layout.mail_fragment);

        initData();
        return getContentView();
    }

    // 初始化条目数据
    private void initData() {
        List<MailBean> mailList = new ArrayList<>();
        Resources resources = getContext().getResources();
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.mail_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setNestedScrollingEnabled(false); // 嵌套ScrollView滑动惯性消失
        String[] names = resources.getStringArray(R.array.mail_name);
        int[] levels = resources.getIntArray(R.array.mail_level);
        int[] icons = new int[]{R.drawable.mail_friend, R.drawable.mail_stranger, R.drawable.mail_system};

        for (int i = 0; i < names.length; i++) {
            MailBean data = new MailBean(i, icons[i], levels[i], names[i]);
            mailList.add(data);
        }
        adapter = new MailFragmentAdapter();
        adapter.setList(mailList);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(this);

        // 消息容器
        LinearLayout msgContainer = (LinearLayout) findViewById(R.id.msg_container);
        mailMsgPanel = new MailMsgPanel(getActivity());
        msgContainer.addView(mailMsgPanel.getContentView());

        MsgMgr.getInstance().attach(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mailMsgPanel != null) {
            mailMsgPanel.refreshList();
        }
    }

    @Override
    public void onItemClick(View convertView, int position) {
        switch (position) {
            case 0:
                MeetUIShow.showMyFriendsAct(getContext());
                break;

            case 1:
                MeetUIShow.showChatStrangerAct(getContext());
                break;

            case 2:
                MeetUIShow.showSysMessActivity(getContext());
                break;
        }
    }

    @Override
    public void onMessage(String key, Object value) {
        switch (key) {
            case MsgType.MT_Unread_change:
                TimerUtil.beginTime(() -> adapter.refreshBadge(), 200);
                break;

            case MsgType.MT_Msg_Notice: // 最新公告消息获取完成
                adapter.refreshNoticeRegister();
                break;

            case MsgType.MT_Msg_Offline: // 离线数据获取成功，重置角标
                adapter.refreshRegister();
                break;
        }
    }
}